package com.cloud.tsp.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.core.exception.MyException;
import com.cloud.api.domain.alarm.AlarmReason;
import com.cloud.tsp.mapper.AlarmReasonMapper;
import com.cloud.tsp.service.IAlarmReasonService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2024-02-21
 */
@Service("alarmReasonService")
public class AlarmReasonServiceImpl extends ServiceImpl<AlarmReasonMapper, AlarmReason> implements IAlarmReasonService {

    @Resource
    private AlarmReasonMapper alarmReasonMapper;

    @Override
    public List<AlarmReason> selectAlarmReasonList(String alarmCategory) {
        return alarmReasonMapper.selectAlarmReasonList(alarmCategory);
    }

    @Override
    public List<AlarmReason> selectAlarmReasonListById(String alarmCategoryId) {
        return alarmReasonMapper.selectAlarmReasonListById(alarmCategoryId);
    }

    @Override
    public void InsertOrUpdate(AlarmReason para) {
        if (StringUtil.isNotBlank(para.getId())){
            para.setAlarmCategory(null);
        }
        this.saveOrUpdate(para);
    }

    @Override
    public void deleteById(String id) {
        AlarmReason alarmReason = alarmReasonMapper.selectById(id);

        LambdaQueryWrapper<AlarmReason> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlarmReason::getAlarmCategory,alarmReason.getAlarmCategory());
        Long count = alarmReasonMapper.selectCount(queryWrapper);

        if (count == 1){
            throw new MyException("wds.exception.deleteAlarmReason");
        }

        alarmReasonMapper.deleteById(id);
    }
}